﻿@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "设备管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshopCode = (ViewBag.WorkshopCode as string) ?? string.Empty;
    var lineId = (int)ViewBag.LineId;
}
@model List<IOA.MES.DataAccess.VModel.VWorkshop>

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkShop}}：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                <option value="">所有车间</option>
                                @foreach (var item in Model)
                                {
                                    <option value="@item.WorkshopCode" @(string.Equals(workshopCode, item.WorkshopCode, StringComparison.CurrentCultureIgnoreCase) ? " selected" : "")>@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.AssemblyLine}}：</label>
                        <div class="layui-input-inline">
                            <select id="selLines" lay-filter="selLines">
                                <option value="0">所有流水线</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Status}}：</label>
                        <div class="layui-input-inline">
                            <select id="selStatus" lay-filter="selStatus">
                                <option value="-1">所有状态</option>
                                <option value="@((int)CommonStatus.Normal)" selected>正常</option>
                                <option value="@((int)CommonStatus.Disabled)">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="editMachine(0);">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'laydate', 'form'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    laydate = layui.laydate,
                    form = layui.form;

                form.on('select(selWorkshops)', function (data) {
                    queryLines(data.value);
                });

                var initLineId = parseInt("@lineId"),
                    initWorkshopCode = "@workshopCode";
                if (initWorkshopCode && initWorkshopCode != "") {
                    queryLines(initWorkshopCode, true);
                }
                function queryLines(workshopCode, fstQuery) {
                    var loadId = layer.load();
                    $.ajax({
                        url: "/Settings/QueryLines",
                        data: {
                            page: 1,
                            limit: 100,
                            workshopCode: workshopCode,
                            status: null
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }

                            var options = ["<option value=\"0\"" + (initLineId <= 0 ? " selected" : "") + ">所有流水线</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                options.push("<option value=\"" + res.Data[i].PkId + "\"" + (initLineId == res.Data[i].PkId ? " selected" : "") + ">" + res.Data[i].LineName + "</option>");
                            }
                            $("#selLines").html(options.join(""));
                            form.render();
                            if (fstQuery) {
                                queryTable();
                            }
                        }
                    });
                }

                var options = {
                    elem: '#table'
                    , url: '/Settings/QueryMachines'
                    , method: "post"
                    , where: { status: 1, workshopCode: initWorkshopCode, lineId: initLineId }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { field: 'PkId', title: 'ID', width: 60 }
                        , { field: 'MachineNo', title: '设备代码' }
                        , { field: 'MachineName', title: '设备名称' }
                        , { field: 'Manufacturers', title: '设备厂家' }
                        , { field: 'MachineModel', title: '设备型号' }
                        , { field: 'WorkshopName', title: '车间名称' }
                        , { field: 'LineName', title: '流水线名称' }
                        , { field: 'CNStatus', title: '状态' }
                        , { field: 'CreateTimeStr', title: '创建时间' }
                        , {
                            title: '操作', width: 200, templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="editMachine(' + d.PkId + ')"><i class="layui-icon">&#xe642;</i>编辑</button>\
                                           <button class="layui-btn layui-btn-sm" onclick="modifyStatus(' + d.PkId + ',' + (1 - d.Status) + ')"><i class="layui-icon">&#xe616;</i>' + (d.Status == 0 ? "启用" : "禁用") + '</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                if (initLineId <= 0) {
                    queryTable();
                }
                function queryTable() {
                    options.page.curr = 1;
                    var status = $("#selStatus").val().trim(),
                        workshopCode = $("#selWorkshops").val().trim(),
                        lineId = parseInt($("#selLines").val().trim());
                    options.where.status = status == "-1" ? null : parseInt(status);
                    options.where.workshopCode = workshopCode;
                    options.where.lineId = lineId;

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function editMachine(machineId) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '500px'],
                content: "/Settings/EditMachine?machineId=" + machineId,
                end: function () {
                    $("#btnSearch").click();
                }
            });
        }

        function modifyStatus(machineId, status) {
            var loadId = layer.load();
            $.ajax({
                url: "/Settings/ModifyMachineStatus",
                data: {
                    machineId: machineId,
                    status: status
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    layer.msg("修改成功！", { icon: 6, time: 1500 });
                    $("#btnSearch").click();
                }
            });
        }
    </script>
}
